package javango.conf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javango.core.Url;
import javango.core.contextprocessors.ContextProcessor;
import javango.middleware.Middleware;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Module;

/**
 *  
 * @author johns
 *
 */
public abstract class BaseSettings implements Settings, Module {
	
	private final static Log log = LogFactory.getLog(BaseSettings.class);

	protected List<Url> urlList = new ArrayList<Url>();
	protected List<Middleware> middlewareList = new ArrayList<Middleware>();
	protected List<ContextProcessor> contextProcessorList = new ArrayList<ContextProcessor>();

	protected Map<String, String> localMap = new HashMap<String, String>();

	public String get(String property) {		
		if (localMap.containsKey(property)) return localMap.get(property);
		return null;
	}
	
	public void set(String property, String value) {
		localMap.put(property, value);
	}

	public String get(String property, String defaultValue) {
		String value = get(property);
		if (value == null) return defaultValue;
		return value;
	}
	
	public boolean isDebug() {
		String value = get("debug");
		if (value == null) {
			return false;
		}
		return (StringUtils.equalsIgnoreCase("true", value));		
	}

	public List<ContextProcessor> getContextProcessorList() {
		return contextProcessorList;
	}

	public List<Middleware> getMiddlewareList() {
		return middlewareList;
	}

	public List<Url> getUrlList() {
		return urlList;
	}

	
	Injector injector;
	
	public void configure(Binder arg0) {
		arg0.bind(Settings.class).toInstance(this);
	}

	public Injector createInjector(List<Module> moduleList) {
		if (moduleList == null) {
			moduleList = new ArrayList<Module>();
		}
		
		moduleList.add(this);
		
		injector = Guice.createInjector(moduleList);
		return injector;
	}

	
}
